package me.leiusual.sharing.ui.shop

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations.map
import androidx.lifecycle.Transformations.switchMap
import androidx.lifecycle.ViewModel
import androidx.paging.PagedList
import me.leiusual.sharing.api.model.Withdraw
import me.leiusual.sharing.pref.Store
import me.leiusual.sharing.repository.WithdrawRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * author: leiusual
 * create on: 2019-07-31
 * description:
 */
class ShopWithdrawViewModel @Inject constructor(
    private val withdrawRepository: WithdrawRepository,
    val store: Store
) : ViewModel() {

  // 提现列表

  private val _withdrawQuery = MutableLiveData<WithdrawQueryParams>()
  val withdrawQueryParams: LiveData<WithdrawQueryParams> = _withdrawQuery
  private val _listing = map(_withdrawQuery) {
    withdrawRepository.withdrawQuery(it.storeId)
  }
  val loadInitial: LiveData<Data<Unit>> = switchMap(_listing) {
    it.loadInitial
  }
  val withdrawQuery: LiveData<PagedList<Withdraw>> = switchMap(_listing) {
    it.pagedList
  }

  fun withdrawQuery(storeId: Long, first: Boolean) {
    _withdrawQuery.value = WithdrawQueryParams(storeId, first)
  }

  data class WithdrawQueryParams(val storeId: Long, val first: Boolean)

  // 申请

  private val _withdrawApply = MutableLiveData<Long>()
  val withdrawApply: LiveData<Data<Unit>> = switchMap(_withdrawApply) {
    withdrawRepository.withdrawApply(it).toLiveData()
  }

  fun withdrawApply(wcId: Long) {
    _withdrawApply.value = wcId
  }

  // 撤销

  private val _withdrawCancel = MutableLiveData<Long>()
  val withdrawCancel: LiveData<Data<Unit>> = switchMap(_withdrawCancel) {
    withdrawRepository.withdrawCancel(it).toLiveData()
  }

  fun withdrawCancel(wcId: Long) {
    _withdrawCancel.value = wcId
  }

  // 删除

  private val _withdrawDelete = MutableLiveData<Long>()
  val withdrawDelete: LiveData<Data<Unit>> = switchMap(_withdrawDelete) {
    withdrawRepository.withdrawDelete(it).toLiveData()
  }

  fun withdrawDelete(wcId: Long) {
    _withdrawDelete.value = wcId
  }
}